<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\BrandCreateRequest;
use App\Http\Requests\BrandGetListRequest;
use App\Models\Brand;
use App\Models\Event;
use App\Utils\HelperUtil;
use Illuminate\Support\Facades\DB;

class BrandController extends Controller
{
    public function store(BrandCreateRequest $request)
    {
        Brand::create($request->all());

        return HelperUtil::responseSuccess(null);
    }

    public function show(Brand $brand)
    {
        return HelperUtil::responseSuccess($brand);
    }

    public function update(BrandCreateRequest $request, Brand $brand)
    {
        $brand->update($request->all());

        return HelperUtil::responseSuccess(null);
    }

    public function destroy(Brand $brand)
    {
        return DB::transaction(function () use ($brand) {
            Event::addEvent(
                $brand->id,
                Event::BRAND_DEL,
                $brand->toArray());
            $brand->delete();
            return HelperUtil::responseSuccess(null);
        });
    }

    public function all()
    {
        return HelperUtil::responseSuccess(Brand::all());
    }

    public function getList(BrandGetListRequest $request)
    {
        $page = Brand::paginate($request->limit);
        return HelperUtil::responseSuccess($page);
    }
}
